/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.snda.dw.lidejia.partitionfilegenerator;

import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.serde2.binarysortable.OutputByteBuffer;

/**
 *
 * @author lidejia
 */
public class PartitionerMeta {

    private int index;
    private boolean invert;
    private String type;

    public PartitionerMeta(int index, boolean invert, String type) {
        this.index = index;
        this.invert = invert;
        this.type = type;
    }

    public void parse(String tokens[], OutputByteBuffer buffer) {
        String data = tokens[index];
        PartitionerMetaParser parser = PartitionerMetaParser.getInstance();
        parser.parse(this, data, buffer);
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public boolean isInvert() {
        return invert;
    }

    public void setInvert(boolean reverse) {
        this.invert = reverse;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public static List<PartitionerMeta> parseMetas(Configuration conf) {
        String[] partitionerColumns = conf.get("totalsort.partitioner.columns").split(";");
        List<PartitionerMeta> metas = new ArrayList<PartitionerMeta>(partitionerColumns.length);
        //
        for (String partitionerColumn : partitionerColumns) {
            String[] tokens = partitionerColumn.split(":");
            PartitionerMeta meta = new PartitionerMeta(Integer.parseInt(tokens[0]), tokens[2].equals("desc"), tokens[1]);
            metas.add(meta);
        }
        //
        return metas;
    }
}
